
CC 		:= arm-none-eabi-gcc
LD 		:= arm-none-eabi-ld
OBJCOPY := arm-none-eabi-objcopy
GDB  	:= arm-none-eabi-gdb
CFLAGS 	:= -DAG10K -mcpu=cortex-m0 -mthumb -g
LDFLAGS := -T./inc/ag10k.ld
DIRS := ./inc

GDB_SCRIPT := gdb/load_sram
ELF := main.elf
BIN := mcu.bin
TCL := mcu.tcl
SRAM_TCL := tcl/load_sram.tcl
MCU_FLASH_ADDR := 0x80000


C_SRC += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
OBJS := $(C_SRC:.c=.o)

%.o:%.c
	$(CC) $(CFLAGS) -c $< -o $@

$(ELF):$(OBJS)
	$(LD) $(LDFLAGS) $^ -o $@

$(BIN):$(ELF)
	$(OBJCOPY) -O binary $(ELF) $(BIN)

$(TCL):$(BIN)
	lua -e "write_pos=$(MCU_FLASH_ADDR)" tools/mcu_2ocdtcl.lua $< >$(@)

all:$(TCL)
	echo $(<)

clean:
	-rm $(ELF) $(BIN) $(TCL) $(OBJS) $(SRAM_TCL)

$(GDB_SCRIPT):$(BIN)
	lua tools/gen_gdb_load.lua $@ $<

$(SRAM_TCL):$(BIN)
	lua -e "write_pos=0x1fff8000;" tools/gen_sram_load.lua $@ $<

sram_tcl:$(SRAM_TCL)

# write_mcu只能通过fpga自带的jtag接口调用
write_mcu:$(TCL)
	openocd -f link.cfg -f flash_read.tcl -f $(<) -c "exit"

# 可以使用link_mcu.cfg调用, 自定义jtag接口
write_sram:$(SRAM_TCL)
	openocd -f link_mcu.cfg -f $(SRAM_TCL)



